//
//  StoreViewController.m
//  JiZhuiAn
//
//  Created by Easy.Lin on 16/6/19.
//  Copyright © 2016年 Easy. All rights reserved.
//

#import "StoreViewController.h"
#import "StoreTableViewCell.h"
#import "SearchCommodityView.h"
#import "SortCommodityView.h"
#import "APIService+Login.h"
#import "AppDelegate.h"

@interface StoreViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableview;
@property (nonatomic, strong) SearchCommodityView * searchView;
@property (nonatomic, strong) SortCommodityView * sortView;

@property (nonatomic, strong) StoreViewModel * viewModel;

@end

@implementation StoreViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewSetting
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"ic_quit"] style:UIBarButtonItemStyleDone target:self action:@selector(logout)];
    [self.view addSubview:self.searchView];
    [self.view addSubview:self.sortView];
    [self.view addSubview:self.tableview];
    [self.viewModel.goodPercent_sortCommand execute:nil];
    
}

- (void)bindViewModel
{
    @weakify(self);
    [[RACObserve(self.viewModel, dataSource) skip:1] subscribeNext:^(id x) {
        @strongify(self);
        [self.tableview reloadData];
    }];
}

- (void)makeConstaints
{
    WS(weakSelf);
    [self.searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@44);
    }];
    [self.sortView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(weakSelf.searchView.mas_bottom);
        make.height.equalTo(@44);
    }];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(@0);
        make.top.equalTo(weakSelf.sortView.mas_bottom);
    }];
}

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    if ([self.tableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableview setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([self.tableview respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableview setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (void)logout
{
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"退出登录" message:@"是否确定退出登录" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert.rac_buttonClickedSignal subscribeNext:^(NSNumber * x) {
        if ([x integerValue] == 1) {
            [[[APIService shareService] logout] subscribeNext:^(id x) {
                [(AppDelegate *)[UIApplication sharedApplication].delegate showLoginView];
            }];
        }
    }];
    [alert show];
}

#pragma mark - SearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length == 0) {
        return;
    }
    [self.viewModel.searchCommand execute:searchText];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    searchBar.text = @"";
    [self.view endEditing:YES];
}

#pragma mark - TableViewDelegate && DataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [StoreTableViewCell cellHeight];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    //按照作者最后的意思还要加上下面这一段
    
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel.dataSource.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StoreTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[StoreTableViewCell CellReuseIdentifier]];
    if(cell){
        [cell setParam:self.viewModel.dataSource[indexPath.row]];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.view endEditing:YES];
    [self.viewModel.didSelectedCommand execute:indexPath];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (SearchCommodityView *)searchView
{
    if (!_searchView) {
        _searchView = [[SearchCommodityView alloc] init];
        _searchView.searchBar.delegate = self;
        [_searchView.searchBar setShowsCancelButton:YES animated:YES];
    }
    return _searchView;
}

- (SortCommodityView *)sortView
{
    if (!_sortView) {
        _sortView = [[SortCommodityView alloc] init];
        _sortView.goodPercent_sortButton.rac_command = self.viewModel.goodPercent_sortCommand;
        _sortView.salesNumber_sortButton.rac_command = self.viewModel.salesNumber_sortCommand;
        _sortView.price_sortButton.rac_command = self.viewModel.salesPrice_sortCommand;
    }
    return _sortView;
}

- (UITableView *)tableview
{
    if (!_tableview) {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.tableFooterView = [UIView new];
        [_tableview registerNib:[StoreTableViewCell cellNib] forCellReuseIdentifier:[StoreTableViewCell CellReuseIdentifier]];
    }
    return _tableview;
}

@end
